Skip to content

Spec insert >http to pro lang start #10187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ReveristRealm
Copy link

@ReveristRealm ReveristRealm commented Jul 3, 2025

Description

Converting the Opensearch APIS into the respective language SDK( Starting with Python first)

  • Tested various GET, POST, PUT, DELETE request in cat.md file
  • Doing python first for testing , then moving to javascript
  • Done some test using Opensearch cluster to varify format

Issues Resolved

Code example only showing raw API or curl

Version

List the OpenSearch version to which this PR applies, e.g. 2.14, 2.12--2.14, or all.

Frontend features

If you're submitting documentation for an OpenSearch Dashboards feature, add a video that shows how a user will interact with the UI step by step. A voiceover is optional.

Checklist

  • By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and subject to the Developers Certificate of Origin.
    For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Daniel Jackson and others added 3 commits June 17, 2025 15:30
…of API calls into Language SDK.

Started with python first

- Added "Example_code in utils.rb"
- Added Switch statement for example_code
- implemented example_code.rb to hande logic for each language
- example_code.mustache for templating
- cat-allocation.md will be the starting test file
Signed-off-by: Daniel Jackson <imdanjac@amazon.com>
Copy link

github-actions bot commented Jul 3, 2025

Thank you for submitting your PR. The PR states are In progress (or Draft) -> Tech review -> Doc review -> Editorial review -> Merged.

Before you submit your PR for doc review, make sure the content is technically accurate. If you need help finding a tech reviewer, tag a maintainer.

When you're ready for doc review, tag the assignee of this PR. The doc reviewer may push edits to the PR directly or leave comments and editorial suggestions for you to address (let us know in a comment if you have a preference). The doc reviewer will arrange for an editorial review.

@kolchfa-aws kolchfa-aws added the In progress Issue/PR: The issue or PR is in progress. label Jul 7, 2025
@dzane17
Copy link
Member

dzane17 commented Jul 7, 2025

  • Update all commits with --signoff to fix GitHub DCO check
  • Add a testing section in PR description. Show an example of spec start/end tags with generated content inside

@kolchfa-aws
Copy link
Collaborator

@nhtruong Would be fantastic if you could review this PR at your convenience. Thanks!

@@ -27,6 +27,40 @@ GET /_cat/allocation/{node_id}
```
<!-- spec_insert_end -->

<!-- spec_insert_start
api: snapshot.restore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to use cat.allocation ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea. To run test to make sure this works, i used the cat.allocation file to run multiple examples and forget to change it back when pushing. will fix


require 'json'

class ExampleCode < BaseMustacheRenderer
Copy link
Contributor

@nhtruong nhtruong Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This class will become unmaintainable once we add more languages for this component. Each language's render logic should have its own renderer class. The ExampleCode class should only be responsible for collecting the rendered code examples from different languages and put them in {% catpure %} and {% include %} blocks.
  • The rendering of each language example code is complex enough to warrant a dedicated mustache template instead of 20 string-manipulation methods that call one another like we see here (I have no idea where to start, nor a vague idea of what the output will look like after taking a squick scan of this file). It's worth spending some time reading the Mustache manual first (it's not long at all). So, for Python, create example_code.python.mustache that will be consumed by ExampleCodePython < BaseMustacheRenderer
  • Don't forget to add tests ;-)

end

def rest_lines
@args.raw['rest']&.split("\n")&.map(&:strip) || []
Copy link
Contributor

@nhtruong nhtruong Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid accessing the raw attr of InsertArguments class like this. Instead, create InsertArguments#rest method. The purpose of this class is parsing the component args to be consumed by the render classes. So, this logic you have here is the responsibility of the InsertArguments class, not this Renderer class. This isolation of concerns principle makes code bases (esp large ones) more maintainable (from better testability and readability). It will also increase reusability (You don't have to keep parsing this arg at different places when they are used more than once)

end

# Parses the body from the REST example (only for preserving raw formatting)
def body
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is a parsing logic for the rest arg as well. Once you have a proper InsetArguments#rest. This should be accessed through @args.rest[:body] or @args.rest.body depending on your implementation of #rest.

<!-- spec_insert_start
api: snapshot.restore
component: example_code
rest: GET _nodes/stats/indices
Copy link
Contributor

@nhtruong nhtruong Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The comment below is not about this PR specifically but about the direction of this project)

This rest arg implies that the person editing this file must know how to use this API as they are also responsible for filling in this arg. This will be a tall order especially for more complex APIs with large request bodies (e.g. the search API). This will also cause other issues that this very project is meant to solve:

  • Outdated docs: When an API is updated, say renaming a query string param that appears in the rest arg, the change in the spec will not be propagated to the doc due to the hardcoding of the rest arg.
  • Human errors: There's no mechanism to make sure that human-input rest arg is without error. For example, GET _nodes/stats/indices is used in this cat-allocation.md :))
  • Automation: In the final form of this project, when a new API is added, it will automatically generate an md file for that API through the spec, including all spec-insert components and their args. That means the example_code component should depend on args like rest

The OpenSearch API spec repo has very comprehensive tests for most APIs. These tests are run against actual OS clusters and reviewed by experts. They are also updated along with the spec. This project should construct code examples from those tests to resolve the problems mentioned above. This approach shifts most of the burdens onto the engineer(s) maintaining spec-insert, but I'd argue that it's a much more realistic option in the long run. I doubt the docs team will have the time to construct the rest arg for every API in OpenSearch, and even then they will still have to make sure that updates on the spec will be reflected in the APIs with code_example components.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dblock what do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback, i will take these into consideration moving forward and work towards making the improvements.

call_code
end
end
end
Copy link
Contributor

@nhtruong nhtruong Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some advice regarding good coding practice (not specific to Ruby):

  1. Keep each method short and simple: You can't see the beginning and the end of a method on the same screen (i.e. you have to scroll), the method is too long!
  2. Limit the number of methods in a class/file: The human memory's buffer is very limited, if you have 20 methods that call one another, we start losing track of what your code is doing (And your AI agents will start to hallucinate)

These seems to contradict each other since breaking a big method into smaller ones will increase the number of methods. There's an art in finding a balance for this that you will mater overtime. These go hand in hand with the isolation of concerns and do not repeat yourself (DRY) principles: You have less methods and shorter methods when you apply these principles properly :)

@kolchfa-aws
Copy link
Collaborator

@ReveristRealm Please consider matching the example requests to their corresponding API specs using regular expressions. This will greatly simplify this task: there would be no human intervention required to put in tags manually. Thanks!

@kolchfa-aws kolchfa-aws added Tech review PR: Tech review in progress and removed In progress Issue/PR: The issue or PR is in progress. labels Jul 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tech review PR: Tech review in progress
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants